/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of My Quick Rules.
 *
 * My Quick Rules is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * My Quick Rules is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with My Quick Rules.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.myquickrules.view;

import org.amphiprion.myquickrules.R;
import org.amphiprion.myquickrules.entity.LocalGame;

import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * View used to display a game in the game list.
 * 
 * @author amphiprion
 * 
 */
public class LocalGameSummaryView extends LinearLayout {
	/** the linked game. */
	private LocalGame game;
	private ImageView img;
	private TextView title;
	private TextView detail;
	private TextView creator;

	/**
	 * Construct an account view.
	 * 
	 * @param context
	 *            the context
	 * @param game
	 *            the game entity
	 */
	public LocalGameSummaryView(Context context, LocalGame game) {
		super(context);
		this.game = game;
		LayoutParams lp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		setLayoutParams(lp);
		setBackgroundDrawable(context.getResources().getDrawable(R.drawable.list_item_black_background_states));

		addView(createIcon());

		addView(createAccountLayout());

	}

	/**
	 * @return the collection
	 */
	public LocalGame getLocalGame() {
		return game;
	}

	/**
	 * Create the search icon view.
	 * 
	 * @return the view
	 */
	private View createIcon() {
		img = new ImageView(getContext());
		LayoutParams imglp = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		imglp.gravity = Gravity.CENTER_VERTICAL;
		imglp.rightMargin = 5;
		img.setLayoutParams(imglp);
		img.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.unknown));
		return img;
	}

	/**
	 * Create the account layout view
	 * 
	 * @return the view
	 */
	private View createAccountLayout() {
		LinearLayout accountLayout = new LinearLayout(getContext());
		LayoutParams aclp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 3);
		accountLayout.setOrientation(VERTICAL);
		accountLayout.setLayoutParams(aclp);
		title = new TextView(getContext());
		LayoutParams tlp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

		title.setLayoutParams(tlp);
		title.setText(game.getName());
		title.setTextSize(16);
		title.setTypeface(Typeface.DEFAULT_BOLD);
		title.setTextColor(getContext().getResources().getColor(android.R.color.white));
		accountLayout.addView(title);

		detail = new TextView(getContext());
		tlp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

		detail.setLayoutParams(tlp);
		detail.setText(getResources().getString(R.string.version_resolution) + " " + game.resolution + " / " + getResources().getString(R.string.version_version) + " "
				+ game.version);
		detail.setTextSize(12);
		detail.setTypeface(Typeface.DEFAULT_BOLD);
		detail.setTextColor(getContext().getResources().getColor(android.R.color.darker_gray));
		accountLayout.addView(detail);

		creator = new TextView(getContext());
		tlp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

		creator.setLayoutParams(tlp);
		creator.setText(getResources().getString(R.string.version_creator) + " " + game.creator);
		creator.setTextSize(12);
		creator.setTypeface(Typeface.DEFAULT_BOLD);
		creator.setTextColor(getContext().getResources().getColor(android.R.color.darker_gray));
		accountLayout.addView(creator);

		return accountLayout;
	}

	/**
	 * @param availableVersion
	 */
	public void setRepositoryVersion(String availableVersion) {
		if (availableVersion != null) {
			if (availableVersion.equals(game.version)) {
				img.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.up_todate));
			} else {
				img.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.need_update));
			}
		}
	}

	/**
	 * @param id
	 */
	public void updateLocalGame(LocalGame newGame) {
		img.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.up_todate));
		game.length = newGame.length;
		game.resolution = newGame.resolution;
		game.version = newGame.version;
		game.creator = newGame.creator;
		game.mail = newGame.mail;
		game.setName(newGame.getName());

		title.setText(game.getName());
		detail.setText(getResources().getString(R.string.version_resolution) + " " + game.resolution + " / " + getResources().getString(R.string.version_version) + " "
				+ game.version);
		creator.setText(getResources().getString(R.string.version_creator) + " " + game.creator);

	}

}
